package com.dmwork.service.interaction.controller.front.me;


import com.dmwork.common.base.result.R;
import com.dmwork.common.base.util.JwtInfo;
import com.dmwork.common.base.util.JwtUtils;
import com.dmwork.service.interaction.entity.vo.CommentLikeMessageVo;
import com.dmwork.service.interaction.feign.MessageFeignService;
import com.dmwork.service.interaction.mq.sender.MessageSender;
import com.dmwork.service.interaction.service.FeedCommentLikeService;
import com.dmwork.service.interaction.service.WorkCommentLikeService;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Transactional
@RestController
@RequestMapping("/front/dm_work/interaction/comment_like_message_info/me")
public class FrontMeCommentLikeMessageInfoController {

    @Autowired
    private WorkCommentLikeService workCommentLikeService;
    @Autowired
    private FeedCommentLikeService feedCommentLikeService;
    @Autowired
    private MessageFeignService messageFeignService;
    @Autowired
    private MessageSender messageSender;

    @ApiOperation("获取评论的点赞消息")
    @GetMapping("get_comment_like_message_info_list/{page}/{limit}")
    public R getCommentLikeMessageInfoList(HttpServletRequest request, @PathVariable Integer page, @PathVariable Integer limit) {
        JwtInfo jwtInfo = JwtUtils.getCustomerInfoByJwtToken(request);
        //从消息服务获取当前用户的点赞消息
        R commentLikeMessageListResult = messageFeignService.getCommentLikeMessageList(page, limit);
        if (!commentLikeMessageListResult.getSuccess()) {
            return R.error().message("获取失败");
        }
        int total = (int) commentLikeMessageListResult.getData().get("total");
        if (total == 0) {
            return R.ok().message("暂无数据").data("total", 0);
        }

        JSONArray commentLikeMessageListJsonArray = new JSONArray(new Gson().toJson(commentLikeMessageListResult.getData().get("rows")));
        List<String> workCommentLikeIdList = new ArrayList<>(); //作品部分评论的点赞记录id
        List<String> feedCommentLikeIdList = new ArrayList<>(); //动态部分评论的点赞记录id
        List<CommentLikeMessageVo> commentLikeMessageVoList = new ArrayList<>();
        int unreadCount = 0; //未读消息数量
        for (int i = 0; i < commentLikeMessageListJsonArray.length(); i++) {
            JSONObject commentLikeMessageJSONObject = commentLikeMessageListJsonArray.getJSONObject(i);
            int type = commentLikeMessageJSONObject.getInt("type");
            if (type == 0) {
                workCommentLikeIdList.add(commentLikeMessageJSONObject.getString("likeId"));
            } else if (type == 1) {
                feedCommentLikeIdList.add(commentLikeMessageJSONObject.getString("likeId"));
            }
            //统计未读消息数量
            int readStatus = commentLikeMessageJSONObject.getInt("readStatus");
            if (readStatus == 0) {
                unreadCount++;
            }
        }
        if (workCommentLikeIdList.size() > 0) {
            commentLikeMessageVoList.addAll(workCommentLikeService.getCommentLikeMessageList(workCommentLikeIdList));
        }
        if (feedCommentLikeIdList.size() > 0) {
            commentLikeMessageVoList.addAll(feedCommentLikeService.getCommentLikeMessageList(feedCommentLikeIdList));
        }
        //按照发布时间排序
        Collections.sort(commentLikeMessageVoList, new Comparator<CommentLikeMessageVo>() {
            @Override
            public int compare(CommentLikeMessageVo o1, CommentLikeMessageVo o2) {
                boolean before = o1.getGmtCreate().before(o2.getGmtCreate());
                if (before) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });
        //设置消息的已读状态
        for (CommentLikeMessageVo commentLikeMessageVo : commentLikeMessageVoList) {
            for (int i = 0; i < commentLikeMessageListJsonArray.length(); i++) {
                JSONObject commentLikeMessageJSONObject = commentLikeMessageListJsonArray.getJSONObject(i);
                if (commentLikeMessageJSONObject.getString("likeId").equals(commentLikeMessageVo.getLikeId())) {
                    commentLikeMessageVo.setReadStatus(commentLikeMessageJSONObject.getInt("readStatus"));
                    break;
                }
            }
        }
        //若未读消息数量大于0，则向MQ发送点赞消息已读的消息，让消息系统将未读的点赞消息设置为已读
        if (unreadCount > 0) {
            messageSender.sendCommentLikeReadMessage(jwtInfo.getId(), new HashMap<>());
        }

        return R.ok().data("total", total).data("rows", commentLikeMessageVoList);
    }

}
